<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>用户管理</title>
<block th:replace="commonBase"></block>   
</head>
<script type="text/javascript">
 /*<![CDATA[*/
	var isType = [{id:'1',text:'是'},{id:'0',text:'否'}];
	var isRight = [{id:'1',text:'有审核权'},{id:'0',text:'无审核权'}];
/*]]>*/
</script>	
<body>
<div class="mini-fit">
	<form id="form1" method="post" >
	    <input id="id" name="id" class="mini-hidden" />
		<div style="padding-left:11px;padding-bottom:5px;">
			<table style="table-layout:fixed;">
				<tr>
					<td>用户姓名:</td>
					<td colspan="3"><input name="userName" class="mini-textbox" style="width:355px;"
						required="true" requiredErrorText="不可为空" emptyText="用户姓名" /><span><font color="red">*</font></span>
					</td>
				</tr>
				<tr>
					<td>用户账号:</td>
					<td colspan="3"><input name="userCode" id="userCodeky" class="mini-textbox" style="width:355px;"  onvalidation="onUserCodeValidation"  required="true" requiredErrorText="不可为空" emptyText="用户账号" /><span><font color="red">*</font></span>
					</td>
				</tr>
				<tr>
					<td>密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码:</td>
					<td colspan="3"><input id="passwordky" name="password"  onvalidation="onfzdvalidation"   class="mini-textbox"    style="width:355px;"     emptyText="请输入密码，为空采用原始密码，" /><span></span>
					</td>
				</tr>
				<tr>
					<td>所属机构:</td>
					<td colspan="3">
					<!-- th:attr="url=@{/org/list}" -->
					<input  name="orgId" id="orgId"  class="mini-treeselect" valueField="id" textField="orgName" parentField="parentId" 
					 	 required="true" allowInput="false" 
						 multiSelect="false" width="355px" showRadioButton="false" valueFromSelect="true" showFolderCheckBox="false" expandOnLoad="0" virtualScroll="true" emptyText="请选择所属机构" />
						 <font color="red">*</font>
					</td>
				</tr>
				<tr>
					<td>手&nbsp;机&nbsp;&nbsp;号:</td>
					<td><input name="telephone" class="mini-textbox" style="width:150px;"
					    emptyText="手机号" /></td>
					<td>邮箱:</td>
					<td><input name="email" class="mini-textbox"  style="width:150px;"
						emptyText="邮箱" />
					</td>
				</tr>
				<tr>
    				<td>是否启用:</td>
    				<td colspan="3">
    					<input name="isEnable" class="mini-combobox" valueField="id" textField="text" required="true" allowInput="false" width="355px" requiredErrorText="不可为空" emptyText="请选择是否启用" 
    					data="isType" value="1"/><font color="red">*</font>
    				</td>
    			</tr>
    			<tr>
    				<td>审核权:</td>
    				<td colspan="3">
    					<input name="isRight" class="mini-combobox" valueField="id" textField="text" required="true" allowInput="false" width="355px" requiredErrorText="不可为空" emptyText="请选择..." 
    					data="isRight" value="1"/><font color="red">*</font>
    				</td>
    			</tr>
				<tr>
					<td>备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注:</td>
					<td colspan="3"><input name="remark" class="mini-textarea"
						style="width:355px;" />
					</td>
				</tr>

			</table>
		</div>
	</form>
	</div>
	<div class="mini-toolbar" style="text-align:center;height:25px;padding-top:2px;padding-bottom:8px;" 
        borderStyle="border-left:0;border-bottom:0;border-right:0;">
        <a class="mini-button" onclick="onOk" iconCls="icon-save" style="width:85px;margin-right:20px;">保存</a>
        <span style="display:inline-block;width:25px;"></span>
        <a class="mini-button" onclick="onCancel" iconCls="icon-close" style="width:85px;">关闭</a>
    </div>
</body>
<script type="text/javascript">
 /*<![CDATA[*/
 	mini.parse();
	var action = "new";
	var form = null;
	var tree = null;
	
	var orgNameList = null;
	var userCode =  null;

	
	function setData(data) {
		action = data.action;//获取指令，是新增 还是修改
		form = new mini.Form("#form1");//获取表单对象
		data = mini.clone(data);//获取克隆数据
		var orgId = "";
	    if (action == "edit") {
	    	orgId = data.fwRightUser.orgId;
	        //跨页面传递的数据对象，克隆后才可以安全使用
	        data = mini.clone(data);
	        form.setData(data.fwRightUser);
	        userCode = data.fwRightUser.userCode;
	        mini.get("passwordky").setValue("");
		 	form.setChanged(false);
	    }else{
	    	orgId =  data.orgId;;
	    }
	    orgNameList = data.orgNameList;
	   
	    queryOrg(orgId);//缓存所有组织机构

	}
	
	
		
	
//缓存所有组织机构
function queryOrg(orgId){
	//alert(orgId);
	 tree = mini.get("orgId");
/* 	 $.ajax({
		 url:basePath+"org/list",
		 type: 'POST',
	     async:false,
		 success : function(text) { 
			 orgNameList = mini.decode(text);*/
	  tree.loadList(orgNameList, "id", "parentId");
			 //如果存在orgId
	  if(!!orgId){
       	tree.setValue(orgId);
	  }
		/*  },
		 error : function() {
		 }
	  }); */
}

function onUserCodeValidation(e) {
		if(!userCode || userCode != e.value){
			$.ajax({
				 url:basePath+"user/isExist/"+e.value,
				 type: 'GET',
			     async:false,
				 success : function(text) { 
					 //0表示可用
					 if(text == "0"){
						e.errorText = "用户已存在";
			            e.isValid = false;
					 }
				  },
				 error : function() {
				 }
			});
		}
}
	
	
	
	//新增机构
	function orgAdd(json){
		$.ajax({
	        url: basePath+"user/add",
			type: 'post',
	        data: {data:json},
	        cache: false,
	        success: function (successData) {
	        	var message = mini.decode(successData);
	        	var orgId = message.msg;//获取组织机构ID，用于树节点定位
	        	mini.unmask(document.body);
	        	if(message.type == '200'){
	            	showMsg("机构添加成功","success",800,"center","center",closeWindow());
	        	}else{
	        		showMsg("机构添加失败","warning",800,"center","center",closeWindow);
	        	}
	        	
	        },
	        error: function (XMLHttpRequest, textStatus, errorThrown) {
	        	 mini.unmask(document.body);
	        	 showMsg("机构添加错误","danger",800,"center","center",closeWindow);
	        }
	    });
	}
	//更新机构信息
	function orgUpdate(json){
		$.ajax({
	        url: basePath+"user/update",
			type: 'post',
	        data: { data: json},
	        cache: false,
	        success: function (successData) {
	        	var message = mini.decode(successData);
	        	var orgId = message.msg;//获取组织机构ID，用于树节点定位
	        	mini.unmask(document.body);
	        	if(message.type == '200'){
        			showMsg("机构更新成功","success",800,"center","center",closeWindow());
	        	}else{
	        		showMsg("机构更新失败","warning",800,"center","center",closeWindow);
	        	}
	        },
	        error: function (jqXHR, textStatus, errorThrown) {
	        	mini.unmask(document.body);
	            showMsg("机构更新错误","danger",800,"center","center",closeWindow);
	        }
	    });
	}
	
	
	function onfzdvalidation(e) {
        if (e.isValid) {
        	if("" == e.value){
        		  e.isValid = true;
        	}else{
	        	 var pPattern = new RegExp('(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,20}');
				  if(!pPattern.test(e.value)){
					  $("#passwordmessage").css("color","#FF5722");
					  $("#passwordmessage").html("<i class='fa fa-remove'></i> 密码为8~20位的必须包含字母数字特殊字符组合");
					  e.errorText = "密码为8~20位的必须包含字母数字特殊字符组合";
					  e.isValid = false;
				  }
        	}
        }
    }
	
	function saveData() {
	    var o = form.getData();  
	    form.validate();
	    if (form.isValid() == false) return;
	    var json = mini.encode(o);
	    mini.mask({
	        el: document.body,
	        cls: 'mini-mask-loading',
	        html: '加载中...'
	    });
	    if (action == "edit"){
	        orgUpdate(json);
	    }else{
	    	orgAdd(json);
	    }
	}
	
	
	function onOk(){
		saveData();
	}
	
	function closeWindow(action) {
	    if (action == "close" && form.isChanged()) {
	        if (confirm("数据被修改了，是否先保存？")) {
	            return false;
	        }
	    }
	    if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
	    else window.close();            
	}
	
	function onCancel(){
		closeWindow("close");
	}
/*]]>*/
</script>
</html>